package org.gap.jseed.reflect;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;


public class MethodIterator extends AbstractClassIterator implements Iterator<Method>, Iterable<Method> {

	private Iterator<Method> iterator;

	public MethodIterator(Class<?> javaClass) {
		Collection<Class<?>> classHierarchy = getClassHierarchy(javaClass);
		List<Method> methods = getAllMethods(classHierarchy);
		
		iterator = methods.iterator();
	}

	protected List<Method> getAllMethods(Collection<Class<?>> hierarchy) {
		List<Method> result = new ArrayList<Method>();
		for (Class<?> each : hierarchy) {
			result.addAll(Arrays.asList(each.getDeclaredMethods()));
		}
		return result;
	}

	@Override
	public boolean hasNext() {
		return iterator.hasNext();
	}

	@Override
	public Method next() {
		return iterator.next();
	}

	@Override
	public void remove() {
		throw new NotImplementedException();
	}

	@Override
	public Iterator<Method> iterator() {
		return this;
	}
}
